<template>
  <div id="app">
    <el-row>
      <el-col :span="span">
        <el-form
          v-if="apiModule"
          ref="form"
          v-model="config"
          class="form"
          :rules="rules"
          label-width="100px"
          label-position="right"
        >
          <el-form-item label="json:" prop="json">
            <el-upload
              v-model="fileList"
              :http-request="httpRequest"
              :on-remove="fileRemove"
              action="#"
              :limit="1"
              accept=".json"
            >
              <el-button type="success" size="small">
                上传 json
              </el-button>
            </el-upload>
          </el-form-item>
          <el-form-item label="requestName:" prop="requestName">
            <el-input v-model="config.requestName"></el-input>
          </el-form-item>
          <el-form-item label="apiPrefix:">
            <el-input v-model="config.apiPrefix"></el-input>
          </el-form-item>
          <el-form-item label="importPath:" prop="importPath">
            <el-input v-model="config.importPath"></el-input>
          </el-form-item>
          <el-form-item label="modelLabel:">
            <el-select v-model="config.modelLabel" style="width: 100%">
              <el-option label="是" :value="true"></el-option>
              <el-option label="否" :value="false"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <el-select
          v-if="!apiModule && data.modelLabel"
          v-model="selectValue"
          filterable
          style="width: 100%; margin-bottom: 20px"
        >
          <el-option
            v-for="(key, index) in Object.keys(model)"
            :key="index"
            :label="key"
            :value="key"
          ></el-option>
        </el-select>
        <div class="submit flex justify-around">
          <el-button type="primary" @click="make">
            提 交
          </el-button>
          <el-button type="success" @click="switchModule">
            切 换
          </el-button>
        </div>
        <div v-if="apiModule" class="list">
          <el-collapse accordion>
            <el-collapse-item
              v-for="(name, index) in Object.keys(data)"
              :key="index"
              :title="name"
              :name="name"
            >
              <div v-if="name === 'api'">
                <div
                  v-for="(item, i) in Object.keys(data.api)"
                  :key="i"
                  class="item"
                  :class="active === item ? 'active' : ''"
                  @click="keyClick('api', item)"
                >
                  {{ item }}
                </div>
              </div>
              <div
                v-else
                class="item"
                :class="active === name ? 'active' : ''"
                @click="keyClick(name, name)"
              >
                {{ name }}
              </div>
            </el-collapse-item>
          </el-collapse>
        </div>
      </el-col>
      <el-col :span="24 - span">
        <el-button type="primary" size="small" @click="copy">
          {{
            hasCopy ? "已复制" : "复制"
          }}
        </el-button>
        <pre
          v-if="apiModule"
          class="line-numbers api"
        ><code v-html="result"></code></pre>
        <pre v-else class="line-numbers api vue"><code>{{ result }}</code></pre>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import Prism from 'prismjs'
import { makeForm, service } from './swaggerV1/service.js'
import 'prismjs/themes/prism.css'

const fileList = ref([])
const model = reactive({})
const selectValue = ref('')
const apiModule = ref(true)
const timeout = ref(null)
const hasCopy = ref(false)
const span = ref(6)
const config = reactive({
  json: null,
  requestName: 'request',
  apiPrefix: '',
  importPath: 'import request from \'@/utils/request\'',
  modelLabel: false,
})
const data = reactive({})
const result = ref('')
const active = ref('')
const rules = reactive({
  json: [
    { required: true, message: '请上传swagger文件', trigger: 'blur' },
  ],
  requestName: [
    { required: true, message: '请输入requestName', trigger: 'blur' },
  ],
  importPath: [
    { required: true, message: '请输入importPath', trigger: 'blur' },
  ],
})

function httpRequest() {
  // 添加上传文件的逻辑
}

function fileRemove(file) {
  // 移除上传文件的逻辑
}

function make() {
  // 提交表单的逻辑

}

function switchModule() {
  // 切换模块的逻辑
}

function keyClick(type, key) {
  // 点击键的逻辑
}

function copy() {
  // 复制到剪贴板的逻辑
}

// 在组件挂载时调用
onMounted(() => {

})

// 在组件更新时更新代码高亮
onUpdated(() => {
  Prism.highlightAll()
})
</script>

<style lang="scss" scoped>
#app {
  min-width: 1800px;
  overflow: hidden;
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  .submit {
    padding-bottom: 20px;
    .el-button {
      width: 40%;
    }
  }
  .el-upload {
    width: 100%;
    .el-button {
      width: 100%;
    }
  }
   .el-upload-list {
    .el-upload-list__item {
      text-align: left;
    }
  }
  .list {
    height: 500px;
    border: 1px #ccc dashed;
   .el-collapse-item__header {
      padding-left: 10px;
    }
    .item {
      text-align: left;
      padding: 10px 25px;
      cursor: pointer;
      border-bottom: 1px #f0f0f0 dashed;
    }
    .active {
      background: #67c04b;
      color: #fff;
    }
    overflow-y: scroll;
  }
  pre {
    text-align: left;
    padding: 20px;
    padding-left: 40px;
    background: #282c33;
    color: #ccc;
    border-top: 4px solid #67c04b;
    counter-reset: line;
    white-space: pre-wrap;
    word-break: break-all;
    span {
      display: block;
      line-height: 1.5rem;
    }
     span.token {
      background: none;
    }
  }

  .api {
    font-size: 14px;
    height: calc(92.5vh - 4px);
    margin: 0;
    margin-left: 20px;
    overflow-y: scroll;
    code {
      span {
        text-align: left;
      }
    }
  }
  .vue {
    color: #67c04b;
  }
}
</style>
